/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package businesscard.DAO;

import bc.entity.Quyen;
import bc.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author THICH HOA DONG TIEN
 */
public class QuyenDAO {
        private Session session;
    public QuyenDAO(){
        SessionFactory factory= HibernateUtil.getSessionFactory();
        this.session=factory.getCurrentSession();
    }
   
    /* thêm quyền*/
    public void insertQuyen( String name)
    {
        try{
            session.getTransaction().begin();
            Quyen q= new Quyen(name);
            session.save(q);
            session.flush();
            session.getTransaction().commit();
        }catch (Exception ex)
        {
            if(session.getTransaction().isActive()){
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
    }
    /*xoa quyen*/
    public void deleteQuyen(int quyenid){
        try{
            session.getTransaction().begin();
            Quyen q= (Quyen)session.get(Quyen.class,quyenid);
            session.delete(q);
            session.flush();
            session.getTransaction().commit();
        }catch (Exception ex)
        {
            if(session.getTransaction().isActive()){
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        
    }
    // nga viet
     public List<Quyen> getAllQuyen()
    {  List<Quyen> list =new ArrayList<Quyen>();
        try
        {
            session.getTransaction().begin();
            String sql ="from Quyen ";
            Query query= session.createQuery(sql);
            list=query.list();
            session.flush();
         //   session.getTransaction().commit();
        }catch (Exception ex)
        {
            if(session.getTransaction().isActive()){
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return list;
        
      }
     //phần của Nga
     public Quyen getQuyenById(int id)
     {
         
        try{
            session.getTransaction().begin();
            String sql="from Quyen where idQuyen = ?";
            Query query=session.createQuery(sql);
            query.setInteger(0, id);
         //   query.setString(0,id);            
            Quyen result=(Quyen)query.uniqueResult();
           // int id=result.getIdUser();
            session.flush();
           // session.getTransaction().commit();
           
            return result;
        }catch(Exception e){
            if(session.getTransaction().isActive())
                session.getTransaction().rollback();
            e.printStackTrace();
        }
        return null; 
     }
      public Quyen [] findByQuyenName(String quyenName )
    {
        try
        {
            session.getTransaction().begin();
            String sql ="from Quyen where QUYEN like ? ";
            Query query= session.createQuery(sql);
            query.setString(0,"%"+quyenName+"%");
            List resultList=query.list();
            Quyen[] result =new Quyen[resultList.size()];
            resultList.toArray(result);
            session.getTransaction().commit();
            return result;
        }catch (Exception ex)
        {
            if(session.getTransaction().isActive()){
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return null;
        
      }
       public List<Quyen> Findbyname(String name) {
        session.beginTransaction().begin();
        String sql = "from Quyen where tenQuyen like ?";
        try {
            Query query = session.createQuery(sql);
            query.setString(0,"%"+name+"%");
            List resultlist = query.list();
            session.getTransaction().commit();
            return resultlist;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return null;

    }
      
     public static void main(String[] args) {
    String name="a";
        QuyenDAO qdao= new QuyenDAO();
        Quyen q=new Quyen();
        try{

          Quyen[] rs=qdao.findByQuyenName(name);
          for (int i=0; i<rs.length;i++){
             // System.out.println(rs[i].getTenquyen());
          }
       
        }catch (Exception ex){
            ex.printStackTrace();
            
           
        }
     }
   
}
